﻿using Common;
using DevExpress.XtraEditors;
using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace NursingFormSystem.Padua
{
    public partial class PaduaEdit : Form
    {
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        string Id;
        public PaduaEdit(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            this.LoginUser = loginUser;
            BindControl();
            BindInfo(this.Id);
        }
        public PaduaEdit(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            BindControl();
            BindInfo(this.Id);
        }

        private void BindControl()
        {
            foreach (var c in groupControl2.Controls)
            {
                if (c is CheckEdit)
                {
                    ((CheckEdit)c).CheckedChanged += chkYinSu_CheckedChanged;
                }
            }
            chkYinSuB1.CheckedChanged += chkYinSu_CheckedChanged;
            foreach (var c in groupControl4.Controls)
            {
                if (c is CheckEdit)
                {
                    ((CheckEdit)c).CheckedChanged += chkYinSu_CheckedChanged;
                }
            }
        }

        void chkYinSu_CheckedChanged(object sender, EventArgs e)
        {
            int sum = 0;
            sum += (chkYinSuA1.Checked ? 1 : 0) + (chkYinSuA2.Checked ? 1 : 0) + (chkYinSuA3.Checked ? 1 : 0)
                    + (chkYinSuA4.Checked ? 1 : 0) + (chkYinSuA5.Checked ? 1 : 0) + (chkYinSuA6.Checked ? 1 : 0)
                    + (chkYinSuA7.Checked ? 1 : 0) + (chkYinSuA8.Checked ? 1 : 0) + (chkYinSuA9.Checked ? 1 : 0)
                    + (chkYinSuA10.Checked ? 1 : 0) + (chkYinSuA11.Checked ? 1 : 0) + (chkYinSuA12.Checked ? 1 : 0)
                    + (chkYinSuA13.Checked ? 1 : 0);
            sum += (chkYinSuB1.Checked ? 2 : 0);
            sum += (chkYinSuC1.Checked ? 3 : 0) + (chkYinSuC2.Checked ? 3 : 0) + (chkYinSuC3.Checked ? 3 : 0) + (chkYinSuC4.Checked ? 3 : 0);
            txtAllSumScore.Text = sum.ToString();
        }



        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            PaduaModel model = new PaduaModel();
            model.PaduaS1 = (chkYinSuA1.Checked ? 1 : 0) + "#" + (chkYinSuA2.Checked ? 1 : 0) + "#" + (chkYinSuA3.Checked ? 1 : 0)
                    + "#" + (chkYinSuA4.Checked ? 1 : 0) + "#" + (chkYinSuA5.Checked ? 1 : 0) + "#" + (chkYinSuA6.Checked ? 1 : 0)
                    + "#" + (chkYinSuA7.Checked ? 1 : 0) + "#" + (chkYinSuA8.Checked ? 1 : 0) + "#" + (chkYinSuA9.Checked ? 1 : 0)
                    + "#" + (chkYinSuA10.Checked ? 1 : 0) + "#" + (chkYinSuA11.Checked ? 1 : 0) + "#" + (chkYinSuA12.Checked ? 1 : 0)
                    + "#" + (chkYinSuA13.Checked ? 1 : 0);
            model.PaduaS2 = (chkYinSuB1.Checked ? "1" : "0");
            model.PaduaS3 = (chkYinSuC1.Checked ? 1 : 0) + "#" + (chkYinSuC2.Checked ? 1 : 0)
                + "#" + (chkYinSuC3.Checked ? 1 : 0) + "#" + (chkYinSuC4.Checked ? 1 : 0);
            model.PaduaSumScore = txtAllSumScore.Text;
            model.FormId = (int)FormType.PaduaScore;
            model.FormName = EnumHelper.GetDescription(FormType.PaduaScore);
            record.NursingFormData = XmlUtil.ObjectToXML(model, Encoding.UTF8);
            record.FormId = model.FormId;//表单ID
            record.FormName = model.FormName;//表单NAME
            record.PatientId = PatientInfo.PATIENT_ID;//住院号
            record.VisitId = PatientInfo.VISIT_ID;//住院次数
            record.PatientName = PatientInfo.PATIENT_NAME;//患者名称
            record.BedNo = PatientInfo.BED_NO;//床号
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;//科室号
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;//科室名称
            record.AreaId = PatientInfo.NURSE_CELL_CODE;//病区编号
            record.AreaName = PatientInfo.NURSE_CELL_NAME;//病区名称
            record.AssessmentDate = DateTime.Now;//日期
            record.Id = string.IsNullOrEmpty(this.Id) ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                //电签开始
                if (Base.ESSwitch.Equals("on"))
                {
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, model.FormName + (record.Id > 0 ? "-更新" : "-新增"), model.FormName, record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool res = es.GetElectronicSignatureImage(es, record.NursingFormData, extraInfo, (model.FormId).ToString(), model.FormName, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID);
                    if (!res) { MessageBox.Show("电子签名失败"); return; }
                }
                MessageBox.Show(record.Id == 0 ? "新增成功" : "更新成功");
                if (record.Id > 0) return;
                //新增成功后绑定数据
                this.Id = string.Empty;
                BindInfo(this.Id);
            }
            else
            {
                MessageBox.Show(ret.M);
            }
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 打印
        private void simplequxiao_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Id)) { MessageBox.Show("请先保存"); return; }
            PaduaPrint f = new PaduaPrint(LoginUser, PatientInfo, this.Id);
            f.ShowDialog();
        }
        #endregion

        private void BindInfo(string id)
        {
            this.txtPatientName.Text = PatientInfo.PATIENT_NAME;
            this.txtBedNo.Text = PatientInfo.BED_NO;
            this.txtPatientId.Text = PatientInfo.PATIENT_ID;

            var ret = formService.GetNursingFormRecordOrderByTime(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.PaduaScore, 1);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (string.IsNullOrEmpty(id))
                    record = list[0];
                else
                    record = list.Find(p => p.Id == Convert.ToInt32(id));
                this.Id = record.Id.ToString();
                var NursingFormData = record.NursingFormData;
                PaduaModel model = new PaduaModel();
                model = XmlUtil.XMLToObject<PaduaModel>(NursingFormData, Encoding.UTF8);
                if (!string.IsNullOrEmpty(model.PaduaSumScore))
                    txtAllSumScore.Text = model.PaduaSumScore.ToString();
                if (!string.IsNullOrEmpty(model.PaduaS1))
                {
                    chkYinSuA1.Checked = model.PaduaS1.Split('#')[0].Equals("1");
                    chkYinSuA2.Checked = model.PaduaS1.Split('#')[1].Equals("1");
                    chkYinSuA3.Checked = model.PaduaS1.Split('#')[2].Equals("1");
                    chkYinSuA4.Checked = model.PaduaS1.Split('#')[3].Equals("1");
                    chkYinSuA5.Checked = model.PaduaS1.Split('#')[4].Equals("1");
                    chkYinSuA6.Checked = model.PaduaS1.Split('#')[5].Equals("1");
                    chkYinSuA7.Checked = model.PaduaS1.Split('#')[6].Equals("1");
                    chkYinSuA8.Checked = model.PaduaS1.Split('#')[7].Equals("1");
                    chkYinSuA9.Checked = model.PaduaS1.Split('#')[8].Equals("1");
                    chkYinSuA10.Checked = model.PaduaS1.Split('#')[9].Equals("1");
                    chkYinSuA11.Checked = model.PaduaS1.Split('#')[10].Equals("1");
                    chkYinSuA12.Checked = model.PaduaS1.Split('#')[11].Equals("1");
                    chkYinSuA13.Checked = model.PaduaS1.Split('#')[12].Equals("1");
                }
                if (!string.IsNullOrEmpty(model.PaduaS2))
                    chkYinSuB1.Checked = model.PaduaS2.Equals("1");
                if (!string.IsNullOrEmpty(model.PaduaS3))
                {
                    chkYinSuC1.Checked = model.PaduaS3.Split('#')[0].Equals("1");
                    chkYinSuC2.Checked = model.PaduaS3.Split('#')[1].Equals("1");
                    chkYinSuC3.Checked = model.PaduaS3.Split('#')[2].Equals("1");
                    chkYinSuC4.Checked = model.PaduaS3.Split('#')[3].Equals("1");
                }
            }
            else if (ret.Code == 2) { this.Id = string.Empty; return; }
            else { MessageBox.Show(ret.M); }
        }

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        private void BtnReData_Click(object sender, EventArgs e)
        {
            BindInfo(this.Id);
        }
    }
}